#include "BaseTestFun.h"

#include <algorithm>
#include "HelpFun.h"
#include "DumpLog.h"
namespace Solver
{

BaseTestFun::BaseTestFun(std::size_t dim, FunType_E type, DifficultyLevel_E diffLevel, const std::string &name, const std::string &description):BaseFun(dim, type, diffLevel, 1, name, description)
{

}

BaseTestFun::~BaseTestFun()
{

}

bool BaseTestFun::setOffset(VectorXd_t &offset)
{
    //1.
    if (offset.size() != bestX_.size())
    {
        LogError("偏移变量维度与变量维度不一致");
        return false;
    }

    //2.
    offset_ = offset;
    temp_.resize(offset.size());
    return true;
}

const VectorXd_t &BaseTestFun::getBestX()
{
    //1.
    if (offset_.size() != bestX_.size())
    {
        return bestX_;
    }

    //2.
    std::transform(bestX_.begin(), bestX_.end(), offset_.begin(), temp_.begin(), [](value_t a, value_t b)-> value_t
    {
        return a - b;
    });

    return temp_;
}

value_t BaseTestFun::calcFun(const VectorXd_t &x)
{
    if (offset_.size() == 0)
    {
        return calcMinFun(x);
    }
    else if(offset_.size() != x.size())
    {
        LogError("偏移维度与变量维度不一致");
        return std::numeric_limits<value_t>::max();
    }
    else
    {
        std::transform(x.begin(), x.end(), offset_.begin(), temp_.begin(), [](value_t a, value_t b)-> value_t
        {
            return a + b;
        });

        return calcMinFun(temp_);
    }
}

void BaseTestFun::write(File &fw) const
{
    //1.
    writeBase(fw);

    //2.
    Help::writeVecValue(fw, offset_);
}

void BaseTestFun::read(File &fr)
{
    //1.
    readBase(fr);

    //2.
    Help::readVecValue(fr, offset_);
}

}